package com.gaussian.property.admin.module.biz.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.gaussian.property.admin.module.biz.vo.monitor.PageQueryProjectMonitorRequest;
import com.gaussian.property.admin.module.biz.vo.monitor.ProjectMonitorForm;
import com.gaussian.property.module.biz.entity.Project;
import com.gaussian.property.module.biz.entity.ProjectMonitor;
import com.gaussian.property.module.biz.entity.ProjectMonitorIndicator;
import com.gaussian.property.module.biz.service.IProjectMonitorIndicatorService;
import com.gaussian.property.module.biz.service.IProjectMonitorService;
import com.gaussian.property.module.biz.service.IProjectService;
import com.songqi.common.core.exception.CustomException;
import com.songqi.common.core.utils.BeanCopyUtil;
import com.songqi.common.mybatis.core.query.LambdaQueryWrapperX;
import com.songqi.common.mybatis.core.util.MyBatisUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @description:
 * @author: songqi(13776346982)
 * @date: 2024/9/29 21:52
 */
@Service
public class ProjectMonitorService {
    @Resource
    private IProjectMonitorService iProjectMonitorService;
    @Resource
    private IProjectMonitorIndicatorService iProjectMonitorIndicatorService;
    @Resource
    private IProjectService iProjectService;


    public IPage<ProjectMonitor> pageQueryProjectMonitor(PageQueryProjectMonitorRequest queryRequest) {
        IPage<ProjectMonitor> iPage = MyBatisUtils.buildPage(queryRequest);
        LambdaQueryWrapperX<ProjectMonitor> queryWrapper = new LambdaQueryWrapperX<ProjectMonitor>()
                .likeIfPresent(ProjectMonitor::getProjectName, queryRequest.getProjectName())
                .eqIfPresent(ProjectMonitor::getEnableStatus, queryRequest.getEnableStatus())
                .orderByDesc(ProjectMonitor::getUpdateTime);
        return iProjectMonitorService.page(iPage, queryWrapper);
    }


    @Transactional(rollbackFor = Exception.class)
    public void addProjectMonitor(ProjectMonitorForm projectMonitorForm) {
        Project project = iProjectService.getById(projectMonitorForm.getProjectId());
        if(project == null){
            throw new CustomException("项目id不存在");
        }
        LambdaQueryWrapper<ProjectMonitor> queryWrapper = new LambdaQueryWrapper<ProjectMonitor>()
                .eq(ProjectMonitor::getProjectId, projectMonitorForm.getProjectId());
        if(iProjectMonitorService.count(queryWrapper) > 0) {
            throw new CustomException("项目每日总结配置已存在。请重新选择。");
        }
        ProjectMonitor projectMonitorEntity = new ProjectMonitor();
        projectMonitorEntity.setProjectId(projectMonitorForm.getProjectId());
        projectMonitorEntity.setProjectName(project.getProjectName());
        iProjectMonitorService.save(projectMonitorEntity);
        List<ProjectMonitorIndicator> projectMonitorIndicatorList = new ArrayList<>();
        for(ProjectMonitorForm.IndicatorCondition indicatorCondition: projectMonitorForm.getIndicatorConditionList()){
            LambdaQueryWrapperX<ProjectMonitorIndicator> queryWrapperX = new LambdaQueryWrapperX<ProjectMonitorIndicator>()
                    .eq(ProjectMonitorIndicator::getProjectId, projectMonitorForm.getProjectId())
                    .eq(ProjectMonitorIndicator::getMonitorIndicatorId, indicatorCondition.getMonitorIndicatorId());
            ProjectMonitorIndicator projectMonitorIndicator = iProjectMonitorIndicatorService.getOne(queryWrapperX, false);
            if(projectMonitorIndicator != null){
                BeanCopyUtil.copyProperties(indicatorCondition, projectMonitorIndicator);
            }else {
                projectMonitorIndicator = BeanCopyUtil.copyProperties(indicatorCondition, ProjectMonitorIndicator.class);
                projectMonitorIndicator.setProjectId(projectMonitorForm.getProjectId());
            }
            projectMonitorIndicatorList.add(projectMonitorIndicator);
        }
        iProjectMonitorIndicatorService.saveOrUpdateBatch(projectMonitorIndicatorList);
    }


    public ProjectMonitorForm getById(String projectMonitorId) {
        ProjectMonitor projectMonitor = iProjectMonitorService.getById(projectMonitorId);
        if(projectMonitor == null){
            throw new CustomException("项目每日总结配置不存在");
        }
        ProjectMonitorForm projectMonitorForm = new ProjectMonitorForm();
        projectMonitorForm.setProjectId(projectMonitor.getProjectId());
        LambdaQueryWrapper<ProjectMonitorIndicator> queryWrapper = new LambdaQueryWrapper<ProjectMonitorIndicator>()
                .eq(ProjectMonitorIndicator::getProjectId, projectMonitor.getProjectId());
        List<ProjectMonitorIndicator> projectMonitorIndicatorList = iProjectMonitorIndicatorService.list(queryWrapper);
        projectMonitorForm.setIndicatorConditionList(BeanCopyUtil.copyList(projectMonitorIndicatorList, ProjectMonitorForm.IndicatorCondition.class));
        return projectMonitorForm;
    }

    @Transactional(rollbackFor = Exception.class)
    public void editProjectMonitor(ProjectMonitorForm projectMonitorForm) {
        Project project = iProjectService.getById(projectMonitorForm.getProjectId());
        if(project == null){
            throw new CustomException("项目id不存在");
        }
        LambdaQueryWrapper<ProjectMonitor> queryWrapper = new LambdaQueryWrapper<ProjectMonitor>()
                .eq(ProjectMonitor::getProjectId, projectMonitorForm.getProjectId());
        ProjectMonitor projectMonitor = iProjectMonitorService.getOne(queryWrapper, false);
        if(projectMonitor == null){
            throw new CustomException("项目每日总结配置不存在，请先新增");
        }
        List<ProjectMonitorIndicator> projectMonitorIndicatorList = new ArrayList<>();
        for(ProjectMonitorForm.IndicatorCondition indicatorCondition: projectMonitorForm.getIndicatorConditionList()){
            LambdaQueryWrapperX<ProjectMonitorIndicator> queryWrapperX = new LambdaQueryWrapperX<ProjectMonitorIndicator>()
                    .eq(ProjectMonitorIndicator::getProjectId, projectMonitorForm.getProjectId())
                    .eq(ProjectMonitorIndicator::getMonitorIndicatorId, indicatorCondition.getMonitorIndicatorId());
            ProjectMonitorIndicator projectMonitorIndicator = iProjectMonitorIndicatorService.getOne(queryWrapperX, false);
            if(projectMonitorIndicator != null){
                BeanCopyUtil.copyProperties(indicatorCondition, projectMonitorIndicator);
            }else {
                projectMonitorIndicator = BeanCopyUtil.copyProperties(indicatorCondition, ProjectMonitorIndicator.class);
                projectMonitorIndicator.setProjectId(projectMonitorForm.getProjectId());
            }
            projectMonitorIndicatorList.add(projectMonitorIndicator);
        }
        iProjectMonitorIndicatorService.saveOrUpdateBatch(projectMonitorIndicatorList);
        projectMonitor.setUpdateTime(new Date());
        iProjectMonitorService.updateById(projectMonitor);
    }

    public Integer changeStatus(String projectMonitorId) {
        ProjectMonitor projectMonitor = iProjectMonitorService.getById(projectMonitorId);
        if(projectMonitor == null){
            throw new CustomException("项目每日总结配置id不存在");
        }
        int status = projectMonitor.getEnableStatus() ^ 1;
        projectMonitor.setEnableStatus(status);
        iProjectMonitorService.updateById(projectMonitor);
        return status;
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteProjectMonitor(String projectMonitorId) {
        ProjectMonitor projectMonitor = iProjectMonitorService.getById(projectMonitorId);
        if(projectMonitor == null){
            throw new CustomException("项目每日总结配置id不存在");
        }
        iProjectMonitorService.removeById(projectMonitorId);

        String projectId = projectMonitor.getProjectId();
        LambdaQueryWrapper<ProjectMonitorIndicator> queryWrapper = new LambdaQueryWrapper<ProjectMonitorIndicator>()
                .eq(ProjectMonitorIndicator::getProjectId, projectId);
        iProjectMonitorIndicatorService.remove(queryWrapper);
    }



}
